package Tools
{
	import flash.geom.Point;
	
	public class MathTool
	{
		public function MathTool()
		{
			super();
		}
		//求直线斜率
		public static function slope(point1:Point,point2:Point):Number
		{
			return (point1.y-point2.y)/(point1.x-point2.x);
		}
		//两条直线交点
		public static function intersect(slope1:Number,point1:Point,slope2:Number,point2:Point):Point
		{
			var point:Point=new Point();
			point.x=(slope1*point1.x-slope2*point2.x+point2.y-point1.y)/(slope1-slope2);
			point.y=slope1*(point.x-point1.x)+point1.y;
			return point;
		}
		//取角度绝对值/消除整圈
		public static function absAngle(angle:Number):Number
		{
			angle=angle%360;
			if(angle<0)
			{
				return angle+360;
			}
			return angle;
		}
		//点是否在线段上
		public static function pointOnSegment(start:Point,end:Point,point:Point):Boolean
		{
			var onLine:Boolean=pointOnLine(start,end,point);
			if(!onLine)
			{
				return false;
			}
			if(point.x<start.x&&point.x<end.x)
			{
				return false;
			}
			if(point.x>start.x&&point.x>end.x)
			{
				return false;
			}
			if(point.y<start.y&&point.y<end.y)
			{
				return false;
			}
			if(point.y>start.y&&point.y>end.y)
			{
				return false;
			}
			return true;
		}
		//点是否在直线
		public static function pointOnLine(point1:Point,point2:Point,point:Point):Boolean
		{
			var lineSlope:Number=slope(point1,point2);
			if(Math.floor((point1.y-point.y))==Math.floor(lineSlope*(point1.x-point.x)))
			{
				return true;
			}
			return false;
		}
		//求两点距离
		public static function distance(point1:Point,point2:Point):Number
		{
			var distanceX:Number = point1.x - point2.x;
			var distanceY:Number = point1.y - point2.y;
			return Math.sqrt(distanceX * distanceX + distanceY * distanceY);
		}
		//判断点是否在椭圆内
		public static function pointInEllipse(a:Number,b:Number,point:Point,h:Number=0,k:Number=0):Boolean
		{
			if(Math.pow(point.x-h,2)/Math.pow(a,2)+Math.pow(point.y-k,2)/Math.pow(b,2)<=1)
			{
				return true;
			}
			return false;
		}
		//根据偏移角度求椭圆上一点
		public static function getEllipsePointByAngle(a:Number,b:Number,angle:Number,h:Number=0,k:Number=0):Point
		{
			var point:Point=new Point();
			point.x=a*cosd(angle)+h;
			point.y=b*sind(angle)+k;
			return point;
		}
		//求符号
		public static function sgn(value:Number):Number
		{
			if (value < 0)
			{
				return -1;
			}
			if (value == 0)
			{
				return 0;
			}
			return 1;
		}
		//锁定值
		public static function lockValue(value:Number, min:Number, max:Number):Number
		{
			if (value < min)
			{
				return min;
			}
			if (value > max)
			{
				return max;
			}
			return value;
		}
		//求余弦值
		public static function cosd(angle:Number):Number
		{
			return Math.cos(angleToRadian(angle));
		}
		//正玄值
		public static function sind(angle:Number):Number
		{
			return Math.sin(angleToRadian(angle));
		}
		//求斜边
		public static function getHypotenuseLength(a:Number,b:Number):Number
		{
			return Math.sqrt(a*a+b*b);
		}
		//根据斜率与截距与x求y
		public static function calculateLineY(slopeValue:Number,intercept:Number,x:Number):Number
		{
			return slopeValue*x+intercept;
		}
		//求菱形内部随机点
		public static function getRhombicRandPoint(a:int,b:int):Point
		{
			var coord:Point=new Point();
			
			var tileWidth:int=2;
			var tileHeight:int=1;
			
			var x:int=RandTool.randInt(0,Math.floor(a/tileWidth));
			var y:int=RandTool.randInt(0,Math.floor(b/tileHeight));
			
			coord.x=(x +y) * tileWidth/2;
			coord.y=(x -y) * tileHeight/2;
			
			coord.x+=tileWidth/2;
			
			return coord;
		}
		//截取小数点
		public static function interceptionDecimal(value:Number,median:int):Number
		{
			return Math.floor(value*Math.pow(10,median))/Math.pow(10,median);
		}
		//求一个点相对于另一点的角度
		public static function angle(point1:Point,point2:Point):Number
		{
			return radianToangle(Math.atan2(point2.y - point1.y, point2.x - point1.x));
		}
		//角度转弧度
		public static function angleToRadian(angle:Number):Number
		{
			return angle*(Math.PI/180);
		}
		//弧度转角度
		public static function radianToangle(radian:Number):Number
		{
			return radian/(Math.PI/180);
		}
		//以固定线性速度趋向终点(一维)
		public static function tendTowards(start:Number, end:Number, speed:Number):Number
		{
			if (Math.abs(end - start) < Math.abs(speed))
			{
				return end;
			}
			if (end > start)
			{
				return start + speed;
			}
			return start - speed;
		}
		//以固定线性速度趋向终点(二维)
		public static function tendTowardsVector(start:Point, end:Point, speed:Number):Point
		{
			if(distance(start,end)<speed)
			{
				return end;
			}
			var angle:Number = angle(start,end);
			var targetX:Number = start.x + cosd(angle)*speed;
			var targetY:Number = start.y + sind(angle)*speed;
			return new Point(targetX, targetY);
		}
		//求中点坐标
		public static function midpoint(point1:Point,point2:Point):Point
		{
			return new Point((point1.x+point2.x)/2,(point1.y+point2.y)/2);
		}
		//求ARGB分量值
		public static function splitColor(color:uint):Array
		{
			var alpha:int = color >> 24& 0xFF;
			var red:int = color >> 16 & 0xFF;
			var green:int = color >> 8 & 0xFF;
			var blue:int = color & 0xFF;
			return [red,green,blue,alpha];
		}
		//合并ARGB颜色值
		public static function mergerColor(red:int,green:int,blue:int,alpha:int=0):uint
		{
			return alpha << 24 | red << 16 | green << 8 | blue;
		}
	}
}